package com.rawm.gen.core;


import com.rawm.gen.common.Constant;
import com.rawm.gen.entity.*;
import com.rawm.gen.support.GenerateFactory;

/**
 * @author 啃过雪糕的兔子
 * @to 生成的主类
 * @date 2018年7月1日 上午1:12:21
 * @see:<p>www.aixuegao.cn</p>
 * @since 1.0.1
 */
public class GeneratorBuilder {


    private Controller controller;

    private Dto dto;

    private PageQueryDto pageQueryDto;

    private Dao dao;

    private IService iService;

    private Mapper mapper;

    private ServiceImpl serviceImpl;

    private Entity entity;

    private Struct struct;

    private BaseEntity baseEntity;

    private Vo vo;

    /**
     * @param clazzName 要生成的entity类名
     * @param table     数据库表名称。如：tbl_user
     * @param be        生成的数据库的业务注释(会生成到类的注释里面)
     */
    public GeneratorBuilder(String clazzName, Table table, BaseEntity be) {
        baseEntity = be;

        entity = new Entity(clazzName, null, table.getColumnList());
        entity.setBasePackage(Constant.PKG_PREFIX);
        entity.setPackageName(Constant.PKG_PREFIX + Constant.PKG_ENTITY);
        entity.setTableComment(baseEntity.getTableComment());
        entity.setNow(be.getNow());
        entity.setAuthor(be.getAuthor());
        entity.setTableName(table.getTable());

        dao = new Dao();
        dao.setEntityName(clazzName);
        dao.setPackageName(Constant.PKG_PREFIX + Constant.PKG_DAO);

        iService = new IService();
        iService.setEntityName(clazzName);
        iService.setBasePackage(Constant.PKG_PREFIX);
        iService.setTableComment(baseEntity.getTableComment());
        iService.setAuthor(be.getAuthor());
        iService.setNow(be.getNow());
        iService.setPackageName(Constant.PKG_PREFIX + Constant.PKG_SERVICE);

        serviceImpl = new ServiceImpl();
        serviceImpl.setEntityName(clazzName);
        serviceImpl.setBasePackage(Constant.PKG_PREFIX);
        serviceImpl.setTableComment(baseEntity.getTableComment());
        serviceImpl.setAuthor(be.getAuthor());
        serviceImpl.setNow(be.getNow());
        serviceImpl.setPackageName(Constant.PKG_PREFIX + Constant.PKG_SERVICE_IMPL);

        mapper = new Mapper();
        mapper.setEntityName(clazzName);
        mapper.setBasePackage(Constant.PKG_PREFIX);
        mapper.setColumnList(table.getColumnList());
        mapper.setPackageName(Constant.PKG_PREFIX + Constant.PKG_DAO);

        dto = new Dto(clazzName + "DTO", null, table.getColumnList());
        dto.setPackageName(Constant.PKG_PREFIX + Constant.PKG_DTO);
        dto.setTableComment(baseEntity.getTableComment());
        dto.setAuthor(be.getAuthor());
        dto.setNow(be.getNow());
        dto.setTableName(table.getTable());

        pageQueryDto = new PageQueryDto(clazzName, null, table.getColumnList());
        pageQueryDto.setPackageName(Constant.PKG_PREFIX + Constant.PKG_DTO);
        pageQueryDto.setTableComment(baseEntity.getTableComment());
        pageQueryDto.setAuthor(be.getAuthor());
        pageQueryDto.setNow(be.getNow());
        pageQueryDto.setTableName(table.getTable());

        vo = new Vo(clazzName + "VO", null, table.getColumnList());
        vo.setPackageName(Constant.PKG_PREFIX + Constant.PKG_VO);
        vo.setTableComment(baseEntity.getTableComment());
        vo.setAuthor(be.getAuthor());
        vo.setNow(be.getNow());
        vo.setTableName(table.getTable());


        controller = new Controller(clazzName, null, table.getColumnList());
        controller.setBasePackage(Constant.PKG_PREFIX);
        controller.setPackageName(Constant.PKG_PREFIX + Constant.PKG_CONTROLLER);
        controller.setTableComment(baseEntity.getTableComment());
        controller.setAuthor(be.getAuthor());
        controller.setNow(be.getNow());
        controller.setTableName(table.getTable());


        struct = new Struct();
        struct.setEntityName(clazzName);
        struct.setPackageName(Constant.PKG_PREFIX + Constant.PKG_STRUCT);
        struct.setAuthor(be.getAuthor());
        struct.setNow(be.getNow());
    }

    /**
     * 获取要生成的条件对象，设置要生成的文件。要生成的设置为true，不生成的文件设置为false
     *
     * @return Condition 生成条件
     */
    public static Condition getCondition() {
        return new Condition();
    }

    /**
     * 生成文件
     */
    public void builder(Condition condition) {
        try {
            if (condition.getEntity()) {
                GenerateFactory.createEntity().generateFile(entity);
            }
            if (condition.getDao()) {
                GenerateFactory.createDao().generateFile(dao);
            }
            if (condition.getiService()) {
                GenerateFactory.createIService().generateFile(iService);
            }
            if (condition.getServiceImpl()) {
                GenerateFactory.createServiceImpl().generateFile(serviceImpl);
            }
            if (condition.getMapper()) {
                GenerateFactory.createMapper().generateFile(mapper);
            }
            if (condition.getDto()) {
                GenerateFactory.createDto().generateFile(dto);
            }
            if (condition.getPageQueryDto()) {
                GenerateFactory.createPageQueryDto().generateFile(pageQueryDto);
            }
            if (condition.getController()) {
                GenerateFactory.createController().generateFile(controller);
            }
            if (condition.getStruct()) {
                GenerateFactory.createStruct().generateFile(struct);
            }
            if (condition.getVo()) {
                GenerateFactory.createVo().generateFile(vo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Controller getController() {
        return controller;
    }

    public void setController(Controller controller) {
        this.controller = controller;
    }

    public Dto getDto() {
        return dto;
    }

    public void setDto(Dto dto) {
        this.dto = dto;
    }

    public PageQueryDto getPageQueryDto() {
        return pageQueryDto;
    }

    public void setPageQueryDto(PageQueryDto pageQueryDto) {
        this.pageQueryDto = pageQueryDto;
    }

    public Dao getDao() {
        return dao;
    }

    public void setDao(Dao dao) {
        this.dao = dao;
    }

    public IService getiService() {
        return iService;
    }

    public void setiService(IService iService) {
        this.iService = iService;
    }

    public Mapper getMapper() {
        return mapper;
    }

    public void setMapper(Mapper mapper) {
        this.mapper = mapper;
    }

    public ServiceImpl getServiceImpl() {
        return serviceImpl;
    }

    public void setServiceImpl(ServiceImpl serviceImpl) {
        this.serviceImpl = serviceImpl;
    }

    public Entity getEntity() {
        return entity;
    }

    public void setEntity(Entity entity) {
        this.entity = entity;
    }

    public Vo getVo() {
        return vo;
    }

    public void setVo(Vo vo) {
        this.vo = vo;
    }

    public BaseEntity getBaseEntity() {
        return baseEntity;
    }

    public void setBaseEntity(BaseEntity baseEntity) {
        this.baseEntity = baseEntity;
    }

    /**
     * @author 啃过雪糕的兔子
     * @to 控制生成的文件，个性化生成，如只生成dao.java文件。自由组合生成
     * @date 2018年7月5日 下午11:03:46
     * @see:<p>www.aixuegao.cn</p>
     * @since 1.0.1
     */
    public static class Condition {

        private Boolean mapper;

        private Boolean entity;

        private Boolean dao;

        private Boolean iService;

        private Boolean serviceImpl;

        private Boolean controller;

        private Boolean dto;

        private Boolean pageQueryDto;

        private Boolean struct;

        private Boolean vo;

        public Boolean getStruct() {
            return struct;
        }

        public Condition setStruct(Boolean struct) {
            this.struct = struct;
            return this;
        }

        public Condition() {
            super();
        }

        public Boolean getMapper() {
            return mapper;
        }

        public Condition setMapper(Boolean mapper) {
            this.mapper = mapper;
            return this;
        }

        public Boolean getEntity() {
            return entity;
        }

        public Condition setEntity(Boolean entity) {
            this.entity = entity;
            return this;
        }

        public Boolean getDao() {
            return dao;
        }

        public Condition setDao(Boolean dao) {
            this.dao = dao;
            return this;
        }

        public Boolean getiService() {
            return iService;
        }

        public Condition setiService(Boolean iService) {
            this.iService = iService;
            return this;
        }

        public Boolean getServiceImpl() {
            return serviceImpl;
        }

        public Condition setServiceImpl(Boolean serviceImpl) {
            this.serviceImpl = serviceImpl;
            return this;
        }

        public Boolean getController() {
            return controller;
        }

        public Condition setController(Boolean controller) {
            this.controller = controller;
            return this;
        }

        public Boolean getDto() {
            return dto;
        }

        public Condition setDto(Boolean dto) {
            this.dto = dto;
            return this;
        }

        public Boolean getPageQueryDto() {
            return pageQueryDto;
        }

        public Condition setPageQueryDto(Boolean pageQueryDto) {
            this.pageQueryDto = pageQueryDto;
            return this;
        }

        public Boolean getVo() {
            return vo;
        }

        public Condition setVo(Boolean vo) {
            this.vo = vo;
            return this;
        }
    }
}
